import os
import time
import datetime
import shelve
import codecs
from subprocess import *

os.environ['DJANGO_SETTINGS_MODULE'] = 'ceemtu.settings'
from django.conf import settings
from django.contrib.auth.models import User

from ceemtu.cetcore.models import Archive, PrimitiveArchive, FilePattern, FileExtension, FileGroup

class CeemtuDeamon:
    
    def __init__(self, home, slumber_time = 360):
        self.home = home
        self.slumber_time = slumber_time
        
    def start(self):
        print "CeemtuDeamon is started. Checking for activity every %s seconds." % self.slumber_time
        
        while True:
            counter = 0
            time.sleep(self.slumber_time)
            
            print "Removing stale rsync-processes"
            for subprc in settings.COREQUEUE:
                if subprc.poll() != None:
                    settings.COREQUEUE.remove(subprc)
                    print ".",
            print " done."

            users = os.listdir(settings.USERS_FOLDER)
            for usrname in users:
                print "Checking for user %s..." % usrname
                usr = User.objects.filter(username = usrname)[0]
                archives = Archive.objects.filter(user = usr, name = 'PersonalData_%s' % usrname)
                if not archives:
                    archive = Archive.objects.create(
                        user = usr,
                        name = 'PersonalData_%s' % usrname,
                        path = os.path.join(settings.USERS_FOLDER, usrname),
                        description = 'Your personal area on the Ceemtu server',
                        generate_checksums = True,
                        extract_metadata = True,
                        scan_interval = 20,
                        scan_interval_type = 1,
                        ignore_files_below_size = settings.IGNORE_FILES_BELOW_SIZE,
                        writable = True,
                        available = True,
                        last_update = datetime.datetime.today()
                    )
                    for fg in FileGroup.objects.all():
                        archive.filegroups.add(fg)
                    archive.save()
                    continue
 
            archives = Archive.objects.filter(available = True)
            file_patterns = FilePattern.objects.all()
            total_time = 0
            for archive in archives:
                print "Checking archive", archive.name
                #print archive.needs_scanning_after() < datetime.datetime.today(), archive.needs_scanning_after(), datetime.datetime.today()

                #if archive.needs_scanning_after() < datetime.datetime.today():
                #    continue
        
                start_time = time.time()
                pa = PrimitiveArchive.create_from_db(archive)
                print "Scanning %s for modifications ..." % archive.name
                print "Path:", archive.path
                pa.verbose = True
                #pa.reset()
                pa.check_filesystem()
                #pa.report()
                archive_updated = False
                for change in pa.get_changes():#get_changes(mark_as_processed = True):
                    #print change
                    counter = counter + 1
                    archive_updated = True
                    Archive.process_entry(
                        archive,
                        archive.user,
                        change,
                        archive.get_file_extension_dict(),
                        archive.get_word_index_dict(),
                        file_patterns
                    )
                pa.set_all_to_unchanged()

                archive.last_update = datetime.datetime.today()
                archive.save()
                # TODO : dump archive as xml
                
                print
                print "Checking backup ...",
                if archive.rsync_cmd() != None and archive_updated:
                    #print "!", archive.rsync_cmd()
                    settings.COREQUEUE.append(Popen(archive.rsync_cmd(), stdout=PIPE, stderr=PIPE))
                    print "started."
                else:
                    print "not needed."
                print "All done!"
                total_time = total_time + time.time() - start_time

            print "%s : All done. Processed %s entries in %.2f seconds. Going back to sleep .... " % (time.time(), counter, total_time)

if __name__ == '__main__':
    deamon = CeemtuDeamon('.', 15)
    deamon.start()


    
